const express = require("express");
let HomeRouter = express.Router();
let SongCont = require('../model/log')


HomeRouter.get('/home', (req, res) => {
    res.render("home")
})

HomeRouter.post('/homelog', (req, res) => {
    let id = '';
    let { title, date, content } = req.body;
    SongCont.create({
        title,
        date,
        content
    }).then(data => {

        let bool = true;
        SongCont.find((err, data) => {
            res.render("login", {
                bool,
                data
            })
        });

    })

})

HomeRouter.get('/home/:id', (req, res) => {
    let bool = true
    let id = req.params.id;
    SongCont.deleteOne({ _id: id }, (err, data) => {
        SongCont.find((err, data) => {
            res.render('login', {
                bool,
                data
            })
        })
    })

})
HomeRouter.get('/home/dle/:id', (req, res) => {
    let id = req.params.id;
    SongCont.findById({ _id: id }, (err, data) => {
        console.log(data.title);
        res.render('add', {
            id,
            data
        })
    })

})
HomeRouter.post('/home/del/end/:id', (req, res) => {
    let _id = req.params.id;
    let bool = true;
    let { title, date, content } = req.body;
    SongCont.findById(_id, (err, data) => {
        SongCont.updateMany(data, { title, date, content, _id }, (err, data) => {
            console.log(data);
        })
    })
    SongCont.find(function(err, data) {
        res.render('login', {
            bool,
            data
        })
    })


})
module.exports = HomeRouter